<template>
	<view class="submit page bgf5 pt12">
		<view class="bgff px16 py12 flex flexC" @click="selectAddress" v-if="defaultAddress != null">
			<image class="w45" src="/static/img/icons/local.png" mode="widthFix"></image>
			<view class="flex1 mx12">
				<view class="bold6"><text
						class="mr16">{{defaultAddress.name}}</text><text>{{defaultAddress.phone}}</text></view>
				<view class="fs12 mt6">{{defaultAddress.province}} {{defaultAddress.city}} {{defaultAddress.area}}
					{{defaultAddress.detail}}
				</view>
			</view>
			<image class="arrows" src="/static/img/icons/arrow.png" mode="aspectFill"></image>
		</view>
		<view class="bgff p12 mt12">
			<view class="flex flexC borb pb12" v-for="item in list">
				<image class="w80 r4" :src="item.image" mode="aspectFill"></image>
				<view class="flex1 pl14">
					<view class="mb10">{{item.goods_name}}</view>
					<text class="active r40 px14 pb1">{{item.specTag}}</text>
					<view class="flex flexC flexB c99 mt8">
						<text class="fs20 bold6 cff0">￥{{item.price}}</text>
						<text>×{{item.count}}</text>
					</view>
				</view>
			</view>
			<view class="flex flexC borb py14">
				<text>快递费</text>
				<text class="flex1 c99 pl20"></text>
				<text class="c99">￥{{express_amount}}</text>
			</view>

			<view class="flex flexC borb py14">
				<text>优惠券</text>
				<text class="flex1 cff0 aliR mr10">暂无</text>
				<image class="arrows" src="/static/img/icon/arrow.png" mode="aspectFill"></image>
			</view>

			<view class="aliR pt14">
				<text>共计{{count}}件</text>
				<text class="ml20">小计：</text>
				<text class="fs18 bold6 cff0">￥{{totalPrice}}</text>
			</view>
		</view>

		<view class="fixed flex flexC p12 bgff boxS">
			<view class="flex1">
				<text>实付：</text>
				<text class="fs18 bold6 cff0">￥{{payPrice}}</text>
			</view>
			<button hover-class="none" @click="doSubmit" class="bgff0 r40 fs14 px20 button">提交订单</button>
		</view>

		<!-- 选择地址 -->
		<uni-popup ref="popup" type="bottom">
			<view class="bgff p12 r10 address">
				<view class="flex flexC aliC c00 bold6 mb10">
					<text class="px20 py4"></text>
					<text class="flex1 fs16">选择收货地址</text>
					<text @click="close" class="px10 fs24">×</text>
				</view>
				<radio-group class="list">
					<label class="flex flexC borb py14" v-for="(item, index) in addressList" :key="item.value">
						<radio :value="item.value" :checked="index === current" color="#FF0000"
							@click="selectDefaultAddress(index)" />
						<view class="flex1 pl6" @click="selectDefaultAddress(index)">
							<view>{{item.detail}}</view>
							<view class="fs12 c79 mt4">{{item.name}} {{item.phone}}</view>
						</view>
						<image class="edit p10" src="/static/imgs/icons/edit.png" mode="aspectFill"
							@click="editAddress(index)"></image>
					</label>
				</radio-group>
				<view class="flex flexJC flexC c79 r5 bgf5 py6 mt12" @click="addAddress">
					<image class="add mt4" src="/static/imgs/icons/add.png" mode="aspectFill"></image>
					<view>新增收货地址</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: [],
				defaultAddress: null,
				list: [],
				count: '',
				totalPrice: 0,
				orderNo: '',
				payPrice: 0,
				payType: 'wechat',
				express_amount: 0,
				tempPayPrice: 0


			};
		},

		onLoad(option) {

			var cai = uni.getStorageSync("cai");
			this.list = JSON.parse(cai)
			for (var i = 0; i < this.list.length; i++) {
				this.count += this.list[i].count;
				this.totalPrice += parseFloat(this.list[i].price) * this.list[i].count;
			}
			this.totalPrice = this.totalPrice;
			this.totalPrice = this.totalPrice.toFixed(2);
			this.totalPrice = parseFloat(this.totalPrice);
			this.payPrice = this.totalPrice;
			this.tempPayPrice = this.payPrice;

			console.info("===tempPayPrice======" + this.tempPayPrice);

		},
		onShow() {
			this.getAddressList();
			var payStatus = uni.getStorageSync("payStatus");
			if (payStatus == 1) {
				var orderNo = uni.getStorageSync("orderNo");
				console.info("=========orderNo=======" + orderNo)
				uni.removeStorageSync("payStatus");
				uni.removeStorageSync("orderNo");
				uni.removeStorageSync("cai");
				uni.redirectTo({
					url: '/other/cai/detail?order_no=' + orderNo
				})
			}
		},
		methods: {

			selectType(type) {
				this.payType = type;
			},
			selectDefaultAddress(index) {

				this.defaultAddress = this.addressList[index];
				this.$refs.popup.close();
				that.getFee();
			},

			editAddress(index) {
				var item = this.addressList[index];
				console.info("edit===" + JSON.stringify(item));
				this.$refs.popup.close();
				uni.navigateTo({
					url: '/other/address/edit?item=' + JSON.stringify(item)
				})
			},

			addAddress() {
				this.$refs.popup.close();
				uni.navigateTo({
					url: '/other/address/add'
				})
			},


			getFee() {

				if (this.defaultAddress == null) {
					return;
				}

				let param = {
					addressId: this.defaultAddress.id,
					goods: JSON.stringify(this.list),
				}
				let that = this;

				this.$api.cacFee(param).then(res => {

					if (res.code == 1) {
						that.express_amount = res.data.toFixed(2);

						that.payPrice = parseFloat(that.tempPayPrice) + parseFloat(that.express_amount);
						that.totalPrice = that.payPrice;
					} else {

					}
					console.info(res);
				})

			},

			doSubmit() {

				console.info("======" + JSON.stringify(this.defaultAddress));
				// if (this.defaultAddress == null) {
				// 	uni.showToast({
				// 		title: "请选择地址",
				// 		icon: 'none'
				// 	});
				// 	return;
				// }

				var addressId = "";
				if (this.defaultAddress != null) {
					addressId = this.defaultAddress.id;
				}
				let param = {
					addressId: addressId,
					goods: JSON.stringify(this.list),
					pay_type: this.payType,
				}
				let that = this;

				this.$api.addCaiOrder(param).then(res => {

					if (res.code == 1) {

						that.orderNo = res.data.orderNo;
						uni.setStorageSync("orderNo", that.orderNo)
						//that.wechatPay(res.data.payInfo);
						uni.openEmbeddedMiniProgram({
							'appId': res.data.appId,
							'path': res.data.path
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
					console.info(res);
				})

			},

			wechatPay(payConfig) {
				let that = this;
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: payConfig.payTimeStamp,
					nonceStr: payConfig.paynonceStr,
					package: payConfig.payPackage,
					signType: payConfig.paySignType,
					paySign: payConfig.paySign,
					success: function(res) {

						uni.redirectTo({
							url: '../order/detail?order_no=' + that.order_no
						})

					},
					fail: function(err) {
						console.info(err);

					}
				});
			},



			addAddress() {
				uni.navigateTo({
					url: '../address/add'
				})
			},

			selectAddress() {
				this.$refs.popup.open();
			},
			close() {
				this.$refs.popup.close();
			},

			getAddressList() {
				let that = this;
				this.$api.addressList({}).then(res => {
					console.info(res);
					if (res.code == 1) {
						that.addressList = res.data;
						if (res.data.length > 0) {
							that.defaultAddress = res.data[0];
							that.getFee();
						} else {
							uni.showModal({
								title: "提示",
								content: "您是否添加地址",
								confirmText: "添加",
								complete(res) {
									if (res.confirm) {
										uni.navigateTo({
											url: '../address/add'
										})
									}
								}
							})

						}

					}

				})
			},

			getDefaultAddress() {

				let param = {


				}
				let that = this;
				this.$api.defaultAddress(param).then(res => {

					if (res.code == 1) {
						that.getAddressList();
						that.defaultAddress = res.data;
						if (res.data == null) {
							uni.navigateTo({
								url: '../address/add'
							})
						}
					}
					console.info(res);
				})
			},


		}
	}
</script>

<style lang="scss" scoped>
	.submit {
		padding-bottom: 106rpx;

		.w45 {
			width: 90rpx;
		}

		.w80 {
			width: 160rpx;
			height: 160rpx;
		}

		.w30 {
			width: 60rpx;
		}

		.active {
			border: 2rpx solid #FD449B;
			background-color: #FFD8DF;
			color: #FD449B;
		}

		.txt {
			height: 120rpx;
		}

		.fixed {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100vw;
		}



		.button {
			height: 60rpx;
			line-height: 60rpx;
		}

		.address {
			.edit {
				width: 40upx;
				height: 40upx;
			}

			.add {
				width: 64upx;
				height: 64upx;

			}

			.list {
				max-height: 70vh;
				overflow-y: auto;
			}
		}
	}
</style>